/**
 * OrderController
 *
 * @description :: Server-side logic for managing orders
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */
const async = require('async');

module.exports = {
	createOrder: function (req, res) {
    var json = req.body;
    Order.create(json).then(function (created) {
      console.log('create new order:', created);
      res.ok('ok');
    }).catch(function (err) {
      console.log(err)
    })
  },

  getOrders: function (req, res) {
    Order.find().then(function (records) {
      console.log('get all orders');
      res.send(records);
    }).catch(function (err) {
      console.log(err);
    })
  },

  getOrder: function (req, res) {
    var id = req.params.id;
    Order.find({id: id}).then(function (record) {
      console.log('get order by id:', record);
      res.send(record);
    }).catch(function (err) {
      console.log(err)
    })
  },

  updateOrder: function (req, res) {
    var id = req.params.id;
    var json = req.body;
    Order.update({id: id}, json).then(function (updated) {
      console.log('update order:', updated);
      res.ok('ok');
    }).catch(function (err) {
      console.log(err);
    })
  },

  deleteOrder: function (req, res) {
    var id = req.params.id;
    Order.query('delete from order where id = ?', id, function (err) {
      if (err) console.log(err);
      console.log('delete order');
      res.ok('ok');
    })
  }
};

